package com.guiwu.gteway.industryapp.Device.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


import com.guiwu.gteway.industryapp.Device.module.GateWay;
import com.guiwu.gteway.industryapp.Device.module.GateWayGroup;
import com.guiwu.gteway.industryapp.R;

import java.util.List;

/**
 * Created by 乔冠 on 2017/12/28.  ExpandList的适配器 ExpandList用来显示组信息
 */

public class GateWayExpandAdapter extends BaseExpandableListAdapter {

    private LayoutInflater mInflater = null;
    //private List<String> titleList;
    private List<GateWayGroup> gateWayGroups;
    private Context context;


    public GateWayExpandAdapter(LayoutInflater inflater,List<GateWayGroup> gateWayGroups ,Context context){
        this.gateWayGroups = gateWayGroups;
        this.mInflater = inflater;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return gateWayGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return gateWayGroups.get(groupPosition).getGateWays().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return gateWayGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return gateWayGroups.get(groupPosition).getGateWays().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holder;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.groups_title_item,parent,false);
            holder = new ViewHolderGroup();
            holder.groupTitleView = convertView.findViewById(R.id.title_item);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolderGroup) convertView.getTag();
        }
        String title = gateWayGroups.get(groupPosition).getGroupName();
        holder.groupTitleView.setText(title);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View childView, ViewGroup parent) {
        ViewHolderChild holder;
        GateWay gateWay = gateWayGroups.get(groupPosition).getGateWays().get(childPosition);
        if (childView == null){
            childView = mInflater.inflate(R.layout.gateway_child_item,parent,false);
            holder = new ViewHolderChild();
            holder.childTitleView = childView.findViewById(R.id.child_item_name);
            holder.childStateView = childView.findViewById(R.id.child_item_state);
            childView.setTag(holder);
        }else {
            holder = (ViewHolderChild) childView.getTag();
        }
        String title = gateWay.getGateWayName();
        int state = gateWay.getIsOnline();
        holder.childTitleView.setText(title);
        if (state == 0){//不在线
            holder.childStateView.setText(context.getString(R.string.ds_isOnline_false));
            holder.childStateView.setTextColor(context.getResources().getColor(R.color.red));
        }else { //在线
            holder.childStateView.setTextColor(context.getResources().getColor(R.color.blue));
            holder.childStateView.setText(context.getString(R.string.ds_isOnline_true));
        }
        return childView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView groupTitleView;
      //  ImageView expandMoreArrow;
    }

    class ViewHolderChild {
        TextView childTitleView;
        TextView childStateView;
    }

}
